package com.edais.service.imp.gift;

import com.edais.dao.TurnOutDao;
import com.edais.domain.PoundageCoupon;
import com.edais.domain.User;
import com.edais.domain.gift.GiftPackageGift;
import com.edais.persistence.PoundageCouponMapper;
import com.edais.service.GiftExchangeFunction;
import com.edais.service.UserService;
import com.edais.service.gift.GiftPackageGiftOrderService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.DateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 手续费抵用券
 * Created by haoyx on 2016/11/10.
 */
@Service("feeCouponImpl")
public class FeeCouponImpl implements GiftExchangeFunction {

    private static final Logger logger = Logger.getLogger(FeeCouponImpl.class);

    @Autowired
    private PoundageCouponMapper poundageCouponMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private GiftPackageGiftOrderService orderService;

    @Override
    @Transactional(propagation= Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
    public Map<String,String> apply(Map<String,Object> params) throws Exception {

        Map<String, String> resMap = new HashMap<String, String>();
        String error = validParams(params);
        if(!StringUtils.isEmpty(error)){
            logger.info(error);
            error = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE,
                    Consts.ErrorMsg.MSG00001,error);
            resMap.put("error",error);
            return resMap;
        }

        //参数获取
        GiftPackageGift giftPackageGift = (GiftPackageGift)params.get("giftPackageGift");
        Long packageOrderId = Long.valueOf(params.get("packageOrderId").toString());
        Long userId = Long.parseLong(params.get("userId").toString());
        String mobile = String.valueOf(params.get("mobile"));

        final String logInfo = "手续费抵扣卷" + giftPackageGift.getId() + userId + "-";

        logger.info(logInfo+"开始");

        User user = this.userService.selectUser(userId,mobile);
        if(null == user){
            logger.info(logInfo+"查询不到此用户");
            error = CommonUtil.setResultStringCn(new HashMap<String, Object>(),Consts.ERROR_CODE,
                    Consts.ErrorMsg.MSG00001,"查询不到此用户");
            resMap.put("error",error);
            return resMap;
        }

        Date nowDate =  this.userService.getSysdate();

        //生成订单
        this.orderService.generateOrder(logInfo,packageOrderId,user,giftPackageGift,nowDate);

        String cp_no = String.valueOf(params.get("cp_no"));
        //计算出时间
        String startDateStr = String.valueOf(params.get("startDate"));
        String endDateStr = String.valueOf(params.get("endDate"));
        Date startDate = DateUtil.getExpiryDate(nowDate,startDateStr);
        Date endDate = DateUtil.getExpiryDate(nowDate,endDateStr);

        PoundageCoupon poundageCoupon = new PoundageCoupon();
        poundageCoupon.setUserId(userId);
        poundageCoupon.setStartDate(startDate);
        poundageCoupon.setEndDate(endDate);
        poundageCoupon.setStatus(PoundageCoupon.Status.UNUSED);
        poundageCoupon.setCpNo(cp_no);
        poundageCoupon.setCreatePer("javaservice");
        this.poundageCouponMapper.insert(poundageCoupon);

        logger.info(logInfo+"成功");

        return resMap;
    }

    @Override
    public String validParams(Map<String,Object> params) {
        if(null == params || params.isEmpty())
            return "参数列表不能为空";
        if(!params.containsKey("giftPackageGift") || null == params.get("giftPackageGift"))
            return "参数giftPackageGift不能为空";
        if(!params.containsKey("packageOrderId")  || StringUtils.isEmpty(params.get("packageOrderId")))
            return "参数packageOrderId不能为空";
        if(!params.containsKey("userId")  || StringUtils.isEmpty(params.get("userId")))
            return "参数userId不能为空";
        if(!params.containsKey("mobile")  || StringUtils.isEmpty(params.get("mobile")))
            return "参数mobile不能为空";
        if(!params.containsKey("cp_no")  || StringUtils.isEmpty(params.get("cp_no")))
            return "参数cp_no不能为空";
        if(!params.containsKey("startDate")  || StringUtils.isEmpty(params.get("startDate")))
            return "参数startDate不能为空";
        if(!params.containsKey("endDate")  || StringUtils.isEmpty(params.get("endDate")))
            return "参数endDate不能为空";
        return null;
    }
}
